package com.yjd.comm.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class NumberUtil {

	public static final String FORMAT_1 = "##0";
	public static final String FORMAT_2 = ",##0";
	public static final String FORMAT_3 = "##0.00";
	public static final String FORMAT_4 = ",##0.00";

	/**
	 * 数字格式化
	 * 
	 * @param value
	 * @param decimals
	 * @return
	 */
	public static Double numberFormat(Double value, int decimals) {
		if (value == null) {
			return null;
		}
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(2);
		return Double.valueOf(nf.format(decimals));
	}

	/**
	 * 数字格式化
	 * 
	 * @param n
	 * @param partten
	 * @return
	 */
	public static String formatNumber(Object n, String partten) {
		if (n == null) {
			return null;
		}
		DecimalFormat df1 = new DecimalFormat(partten);
		return df1.format(n);
	}

	/**
	 * 格式化数据
	 * 
	 * @param n
	 * @return
	 */
	public static String formatNumber(Double n) {
		return formatNumber(n, FORMAT_3);
	}

	/**
	 * 格式化数据 ##0
	 * 
	 * @param n
	 * @return
	 */
	public static String formatNumber1(Double n) {
		return formatNumber(n, FORMAT_1);
	}

	/**
	 * 格式化数据,##0
	 * 
	 * @param n
	 * @return
	 */
	public static String formatNumber2(Double n) {
		return formatNumber(n, FORMAT_2);
	}

	/**
	 * 格式化数据##0.00
	 * 
	 * @param n
	 * @return
	 */
	public static String formatNumber3(Double n) {
		return formatNumber(n, FORMAT_3);
	}

	/**
	 * 格式化数据,##0.00
	 * 
	 * @param n
	 * @return
	 */
	public static String formatNumber4(Double n) {
		return formatNumber(n, FORMAT_4);
	}

      
    public static boolean checkNumber(String value){  
        String regex = "^([1-9]\\d*\\.?\\d{0,2})|(0\\.\\d{0,2}[1-9])|([0])|([0]\\.\\d{0,2})$";  
        return value.matches(regex);  
    }  
    
    public static void main(String[] args) {
		System.out.println(checkNumber("12.36"));
	}
}
